<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'can';
$return=array();
$uniacid=$_SESSION['uniacid'];
$mid=$_SESSION['uid'];
$basic=$this->plugin_setting('basic');
$return['basic']=$return['setting']=$basic;
if($_RQ['op']=='can'){
	$id=$_RQ['actid'];
	if(!$id){
		exi('没有找到该活动，请重试！','error');
	}
	$act=pdo_get('jy_yousetli_act',array('uniacid'=>$uniacid,'id'=>$id));
	$member=pdo_get('core_members',array('uniacid'=>$uniacid,'id'=>$mid));
	$player=pdo_get('jy_yousetli_player',array('uniacid'=>$uniacid,'mid'=>$mid,'actid'=>$act['id']));
	//计算我的总优惠	
	$sum =pdo_getsum('jy_yousetli_help',array("uniacid" => $_SESSION['uniacid'],'status <>'=>2,"actid" => $id,"help_mid" => $mid),'money');
	//筛选我可以兑换的优惠券
	$couponids=json_decode($act['couponids'],true);
	$list=array();
	foreach($couponids as $item){
		$coupon=pdo_get('jy_weishop_coupon',array('uniacid'=>$uniacid,'id'=>$item['couponid']));
		if($item['enough']<=$sum){
				$limittimes2=json_decode($coupon['limittimes'],true);
				$coupon=model('jy_weishop/coupon')->intcoupon($coupon['id']);
				if($coupon['issendtime']){
					$coupon['sendtime']=$coupon['sendtime']?json_decode($coupon['sendtime'],true):array();
					$coupon['etime']=$coupon['sendtime']['tend'];
					if($coupon['sendtime']['tstart']>time()){
						$coupon['issend']=0;
						$coupon['etime']=$coupon['sendtime']['tstart'];
					}elseif($coupon['sendtime']['tstart']<time() && $coupon['sendtime']['tend']>time()){
						$coupon['issend']=1;
						$coupon['etime']=$coupon['sendtime']['tend'];
					}elseif($coupon['sendtime']['tend']<time()){
						$coupon['issend']=2;
					}
					$coupon['sendtime']['tend2']=$coupon['sendtime']['tend'];
					$coupon['sendtime']['tstart2']=$coupon['sendtime']['tstart'];
					$coupon['sendtime']['tend']=date('Y-m-d',$coupon['sendtime']['tend']);
					$coupon['sendtime']['tstart']=date('Y-m-d',$coupon['sendtime']['tstart']);
				}else{
					$coupon['issend']=1;
				}
				if($coupon['timelimittype']==2 && $limittimes2['tend']<time()){
					unset($getcoupon[$k]);
				}else{
					
				}
			$list[]=$coupon;
		}
	}
	//判断游戏状态
	$return['coupon_sum']=count($list);;
	$return['player']=$player;
	$return['act']=$act;
	$return['list']=$list;
	$return['member']=$member;
	$return['sum']=$sum;
	$return['sharedata']=array(
		'title'=>$act['title'],
		'desc'=>'我正在参加'.$act['title'].'活动，快来帮我拿优惠',
		'link'=>custom_url('jy_yousetli/detail//id='.$_RQ['id'].'&help_mid='.$mid,true),
		'imgUrl'=>ATTACHMENT_URL.$act['image'],
	);
	set_html('title','可兑换优惠券');
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('mid'=>$_SESSION['uid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	} 
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_yousetli_exchange',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']));
			$act=pdo_get('jy_yousetli_act',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['actid']));
			$play=pdo_get('jy_yousetli_player',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['playid']));
			$coupon=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['couponid']));
			$item['coupon']=$coupon;
			$item['createtime']=date('y-m-d h:i:s',$item['createtime']);
			$item['shenhetime']=date('y-m-d h:i:s',$item['shenhetime']);
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='getcoupon'){
	$couponid=$_RQ['couponid'];
	$actid=$_RQ['actid'];
	pdo_update('jy_yousetli_help',array('status'=>2),array('uniacid'=>$uniacid,"actid" => $actid,"help_mid" => $mid));
	$ins=array(
				'uniacid'=>$uniacid,
				'mid'=>$mid,
				'actid'=>$actid,
				'couponid'=>$couponid,
				'remark'=>'兑换优惠券申请',
				'applytime'=>TIMESTAMP
	);
	pdo_insert('jy_yousetli_exchange',$ins);
	model('jy_weishop/coupon')->getcoupon($couponid);
	exi('兑换优惠券成功');
}